<?php
/**
 * 分页类
 * @author konakona
 * @date 2009-10-21
 */
class page{
	/**
	 * 每页显示数
	 * @var int 
	 */
	public static $each_page=10;
	 /**
     * 获取表的总条数
     * @param string @name
     */
	public static function tableCount($name,$id=false){
		if($id ==false){
			$sql = "select count(*) as amount from `{$name}`";
		}else{
			$sql = "select count(*) as amount from comments where fid = $id";
		}
		$result = mysql::connect()->fetch_all($sql);
		return $result[0]['amount'];
	}
	/**
	 * 设置每页显示数
	 * @param int $page
	 */
	public static function set_Each_page($page){
		self::$each_page = $page;
	}
    /**
     * 中文的分页
     * @param string $pid $tablename $row
     */
	public static function run($pid,$tablename,$url){
		$page = empty($pid)?1:$pid;
		$allcount = self::tableCount($tablename);//14
		if($allcount){
			if($allcount<$page){$page_count = 1;}//如果总数小于$page，那么只有一页
			if($allcount%$page){$page_count = (int)($allcount/self::$each_page)+1;//取总数据量除以每页数的余数
			}else{
			$page_count = ceil($allcount/self::$each_page);	//如果没有余数,则页数等于总数据量除以每页数的结果
			}
		}else{
			$page_count=0;
		}
		$page_string = '';
		if( $page == 1 ){
			$page_string .= "有{$allcount}笔记录  共{$page_count}页 首页 上一页";
		}
		else{
			$page_string .= "有{$allcount}笔记录  共{$page_count}页 <a href=".k::$config['WEB_URL']."{$url}=1>首页</a> <a href=".k::$config['WEB_URL']."{$url}".($page-1).">上一页</a> ";
		}
		if( ($page == $page_count) || ($page_count == 0) ){
			$page_string .= '下一页 尾页';
		}
		else{
			$page_string .= "<a href=".k::$config['WEB_URL']."{$url}".($page+1)."{$px}>下一页</a> <a href=".k::$config['WEB_URL']."{$url}{$page_count}{$px}>尾页</a>";
		}
		return $page_string;
	}
	/**
     * 英文格式执行函数
     * @param string $pid $tablename $url $id
	 * @return string
     */
	public static function enrun($pid,$tablename,$url,$id=false){
		$page = empty($pid)?1:$pid;
		$allcount = self::tableCount($tablename,$id);//14
		if($allcount){
			if($allcount<$page){$page_count = 1;}//如果总数小于$page，那么只有一页
			if($allcount%$page){$page_count = (int)($allcount/self::$each_page)+1;//取总数据量除以每页数的余数
			}else{
			$page_count = ceil($allcount/self::$each_page);	//如果没有余数,则页数等于总数据量除以每页数的结果
			}
		}else{
			$page_count=0;
		}
		$page_string = '';
		if( $page == 1 ){
			$page_string .= "Total:{$page_count} Frist Previous";
		}
		else{
			$page_string .= "Total:{$page_count} <a href=".k::$config['WEB_URL']."{$url}-1.html>Frist</a> <a href=".k::$config['WEB_URL']."{$url}-".($page-1).".html>Previous</a> ";
		}
		if( ($page == $page_count) || ($page_count == 0) ){
			$page_string .= 'Next End';
		}
		else{
			$page_string .= "<a href=".k::$config['WEB_URL'].$url."-".($page+1).".html>Next</a> <a href=".k::$config['WEB_URL'].$url."-".$page_count.".html>End</a>";
		}
		return $page_string;
	}
}